#!/bin/bash

set -e

# just in case...
unset CC
unset CXX

#PREFIX=i686-w64-mingw32
PREFIX=x86_64-w64-mingw32

#export PATH="/usr/$PREFIX/bin:$PATH"
export INSTALLDIR="$PWD/$PREFIX"
export PKG_CONFIG_PATH=$INSTALLDIR/lib/pkgconfig:/usr/$PREFIX/lib/pkgconfig
export CPPFLAGS="-I$INSTALLDIR/include"
export LDFLAGS="-L$INSTALLDIR/lib"

# let mingw compiler be less verbose
export CFLAGS="-O2 -Wall -Wno-format"

# build libunistring
if [ ! -d libunistring ]; then
  git clone https://git.savannah.gnu.org/git/libunistring.git
  cd libunistring
else
  cd libunistring
  git pull
fi
./autogen.sh
./configure --build=x86_64-pc-linux-gnu --host=$PREFIX --enable-shared --prefix=$INSTALLDIR
make clean
make -j$(nproc)
make install
cd ..

# build libidn2
if [ ! -d libidn2 ]; then
  git clone https://gitlab.com/libidn/libidn2.git
  cd libidn2
else
  cd libidn2
  git pull
fi
./bootstrap
./configure --build=x86_64-pc-linux-gnu --host=$PREFIX --enable-shared --disable-doc --disable-gcc-warnings --prefix=$INSTALLDIR
make clean
make -j$(nproc)
make install
cd ..

# build libpsl
export WINEPATH="$INSTALLDIR/bin;$INSTALLDIR/lib;/usr/$PREFIX/bin;/usr/$PREFIX/lib;$PWD/libpsl/.libs;$GCCLIB"
./autogen.sh
./configure --build=x86_64-pc-linux-gnu --host=$PREFIX --enable-shared --disable-gtk-doc --enable-runtime=libidn2 --enable-builtin=libidn2 --prefix=$INSTALLDIR
make clean
make -j$(nproc)
make check -j$(nproc) LOG_COMPILER=wine
